import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class ValueMetric {
    public void valueMetric(JSONArray jsonArrayCells, List<Packages> packages, MetricValues metricValues[], AllMetric allMetric){

        // 计算加权结果，加入call、extend、implement值后的结果
        int srcNum, destNum, srcPackageNum, destPackageNum;
        System.out.println("计算中...");

        // 加权计算
        for(int i = 0; i < jsonArrayCells.length(); i++){
            //System.out.println("遍历第" + (i + 1) + "个values");
            JSONObject cell = (JSONObject) jsonArrayCells.get(i);
            JSONObject values = cell.getJSONObject("values");

            srcNum = cell.getInt("src");
            destNum = cell.getInt("dest");
            srcPackageNum = (new PackageNum()).packageNum(srcNum, packages);
            destPackageNum = (new PackageNum()).packageNum(destNum, packages);

            // 存在调用call
            if(values.has("Call")) {
                allMetric.usesSum += values.getInt("Call");
                metricValues[srcPackageNum].packageUsesSum += values.getFloat("Call");
                if(srcPackageNum != destPackageNum){
                    allMetric.difUsesSum += values.getInt("Call");
                    metricValues[srcPackageNum].difPackageUsesSum += values.getFloat("Call");
                    //metricValues[srcPackageNum].difPackageClientSP += values.getFloat("Call");  // 包更改影响指数
                    if(!metricValues[srcPackageNum].difPackageUsesP.contains(destPackageNum)){
                        metricValues[srcPackageNum].difPackageUsesP.add(destPackageNum);
                        metricValues[srcPackageNum].difPackageUsesPNum++;
                    }

                    // 内聚度指标记录 P的客户包、提供者包
                    if(!metricValues[destPackageNum].packageClientsP.contains(srcPackageNum)){
                        metricValues[destPackageNum].packageClientsP.add(srcPackageNum);
                    }
                    if(!metricValues[srcPackageNum].packageProviderP.contains(destPackageNum)){
                        metricValues[srcPackageNum].packageProviderP.add(destPackageNum);
                    }
                }
            }
            // 存在扩展使用 出现extend/implement
            if(values.has("Extend")||values.has("Implement")) {
                if(values.has("Extend")){
                    allMetric.extSum += values.getInt("Extend");
                    metricValues[destPackageNum].packageExtSum += values.getFloat("Extend");
                }
                if(values.has("Implement")){
                    allMetric.extSum += values.getInt("Implement");
                    metricValues[destPackageNum].packageExtSum += values.getFloat("Implement");
                }
                if(srcPackageNum != destPackageNum){
                    if(values.has("Extend")){
                        allMetric.difExtSum += values.getInt("Extend");
                        metricValues[destPackageNum].difPackageExtSum += values.getFloat("Extend");
                    }
                    if(values.has("Implement")){
                        allMetric.difExtSum += values.getInt("Implement");
                        metricValues[destPackageNum].difPackageExtSum += values.getFloat("Implement");
                    }
                    //metrics[destPackageNum].difPackageClientSP++;  //包更改影响指数
                    if(!metricValues[destPackageNum].difPackageExtP.contains(srcPackageNum)){
                        metricValues[destPackageNum].difPackageExtP.add(srcPackageNum);
                        metricValues[destPackageNum].difPackageExtPNum++;
                    }

                    // 内聚度指标记录 P的客户包、提供者包
                    if(!metricValues[destPackageNum].packageClientsP.contains(srcPackageNum)){
                        metricValues[destPackageNum].packageClientsP.add(srcPackageNum);
                    }
                    if(!metricValues[srcPackageNum].packageProviderP.contains(destPackageNum)){
                        metricValues[srcPackageNum].packageProviderP.add(destPackageNum);
                    }
                }
            }
        }

        // 第二次遍历 计算P的输入接口
        System.out.println("第二遍遍历");
        for(int i = 0; i < jsonArrayCells.length(); i++){
            JSONObject cell = (JSONObject) jsonArrayCells.get(i);
            JSONObject values = cell.getJSONObject("values");

            srcNum = cell.getInt("src");
            destNum = cell.getInt("dest");
            srcPackageNum = (new PackageNum()).packageNum(srcNum, packages);
            destPackageNum = (new PackageNum()).packageNum(destNum, packages);

            // 存在调用call
            if(values.has("Call")||values.has("Extend")||values.has("Implement")) {
                if(srcPackageNum != destPackageNum){
                    // 内聚度指标记录
                    if(!metricValues[srcPackageNum].packageInIntC.contains(srcNum) && metricValues[srcPackageNum].packageClientsP.contains(destPackageNum)){
                        metricValues[srcPackageNum].packageInIntC.add(srcNum);
                    }
                }
            }
        }

        // 第三次遍历 计算P的RoleClientsSum， 统计包P的 提供不
        System.out.println("第三遍遍历");
        for(int i = 0; i < jsonArrayCells.length(); i++){
            JSONObject cell = (JSONObject) jsonArrayCells.get(i);
            JSONObject values = cell.getJSONObject("values");

            srcNum = cell.getInt("src");
            destNum = cell.getInt("dest");
            srcPackageNum = (new PackageNum()).packageNum(srcNum, packages);
            destPackageNum = (new PackageNum()).packageNum(destNum, packages);

            // 存在调用call
            if(values.has("Call")||values.has("Extend")||values.has("Implement")) {
                if(srcPackageNum != destPackageNum){
                    // 内聚度指标记录
                    // 包目标集中指数计算 计算InIntPQ, RoleClientsSum
                    if(metricValues[destPackageNum].packageInIntC.contains(destNum)){
                        int leap = 1; // 设置标志变量
                        for(InIntPC inIntPC : metricValues[destPackageNum].inIntPC){ // 检查是否已经存储过InIntPQ
                            if(inIntPC.classNum == destNum && inIntPC.packageNum == srcPackageNum){
                                leap = 0; // 如果找到，将leap置0
                                break;
                            }
                        }
                        if(leap == 1){
                            InIntPC inIntPC = new InIntPC();
                            inIntPC.packageNum = srcPackageNum;
                            inIntPC.classNum = destNum;
                            metricValues[destPackageNum].inIntPC.add(inIntPC);
                            metricValues[destPackageNum].RoleClientsSum ++;

                            if(metricValues[destPackageNum].providerCSPQ.containsKey(destPackageNum)){
                                metricValues[destPackageNum].providerCSPQ.put(destPackageNum, metricValues[destPackageNum].providerCSPQ.get(destPackageNum) + destNum);
                            }else {
                                metricValues[destPackageNum].providerCSPQ.put(srcPackageNum, destNum + "");
                            }
                        }
                    }
                }
            }
        }
    }
}
